﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinkCard.Model;

namespace LinkCard.Dao
{
    public class AlbumDao: ITopLinkDao
    {
        public int Add(object obj)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objAdd = (Album) obj;
                db.AddToAlbums(objAdd);
                rs = db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public int Update(object obj)
        {
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objUpdate = (Album)obj;
                var id = objUpdate.AlbumID;
                var oldObj = db.Albums.FirstOrDefault(t => t.AlbumID == id);
                if (oldObj != null)
                {
                    oldObj.Album1 = objUpdate.Album1;
                    rs = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public int Delete(object obj)
        {            
            var rs = 0;
            try
            {
                var db = new TopLinkEntities();
                var objDel = (Album)obj;
                var id = objDel.AlbumID;
                var oldObj = db.Albums.FirstOrDefault(t => t.AlbumID == id);
                if (oldObj != null)
                {
                    db.DeleteObject(oldObj);
                    rs = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rs;
        }

        public Album GetById(long id)
        {
            var al = new Album();
            try
            {
                var db = new TopLinkEntities();
                al = db.Albums.FirstOrDefault(t => t.AlbumID == id);                
            }
            catch (Exception)
            {                
                throw;
            }
            return al;
        }
    }
}
